<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="description" content="">
  <meta name="keywords" content="">
  
    <link rel="icon" href="/avatar.jpg">
  
    
  <title>HDU-1166 敌兵布阵 | 科学君的不科学博客</title>
  <link rel="stylesheet" href="/style.css">
  <link rel="stylesheet" href="/lib/jquery.fancybox.min.css">
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <script type="text/javascript" src="https://tajs.qq.com/stats?sId=65546304" charset="UTF-8"></script>
  <script>
  (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
          bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      }
      else {
          bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>
</head>

<body>
  <header>
  <div class="header-container">
    <a class='logo' href="/">
      <span>科学君的不科学博客</span>
    </a>
    <ul class="right-header">
      
        <li class="nav-item">
          
            <a href="/" class="item-link">首页</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/about" class="item-link">关于</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/archives" class="item-link">归档</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/tags" class="item-link">标签</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/atom.xml" class="item-link">FEED 订阅</a>
          
        </li>
      
    </ul>
  </div>
</header>

  <main id='post'>
  <div class="content">
    <article>
      <section class="content markdown-body">
        <h1>HDU-1166 敌兵布阵</h1>
        <div class='post-meta'>
          <i class="fa fa-calendar" aria-hidden="true"></i>
          <time>2016年04月14日</time>
          
          | <i class="fa fa-folder-open-o" aria-hidden="true"></i> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/acm/">acm</a>
  </div>



          
          
          |
          
          <i class="fa fa-tags" aria-hidden="true"></i>
          
          
  <a href="/tags/#acm" class='tag'>acm</a>

  <a href="/tags/#线段树" class='tag'>线段树</a>


          
        </div>
        <h1 id="题目："><a href="#题目：" class="headerlink" title="题目："></a>题目：</h1><p class="verse"><br>敌兵布阵<br><br>Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)<br><br>Total Submission(s): 69427    Accepted Submission(s): 29197<br><br><br><br><br><br>Problem Description<br><br>C 国的死对头 A 国这段时间正在进行军事演习，所以 C 国间谍头子 Derek 和他手下 Tidy 又开始忙乎了。A 国在海岸线沿直线布置了 N 个工兵营地,Derek 和 Tidy 的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段，所以每个工兵营地的人数 C 国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动，可能增加或减少若干人手,但这些都逃不过 C 国的监视。<br><br>中央情报局要研究敌人究竟演习什么战术,所以 Tidy 要随时向 Derek 汇报某一段连续的工兵营地一共有多少人,例如 Derek 问:“Tidy,马上汇报第 3 个营地到第 10 个营地共有多少人!”Tidy 就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动，而 Derek 每次询问的段都不一样，所以 Tidy 不得不每次都一个一个营地的去数，很快就精疲力尽了，Derek 对 Tidy 的计算速度越来越不满:”你个死肥仔，算得这么慢，我炒你鱿鱼!”Tidy 想：“你自己来算算看，这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下，Tidy 只好打电话向计算机专家 Windbreaker 求救,Windbreaker 说：“死肥仔，叫你平时做多点 acm 题和看多点算法书，现在尝到苦果了吧!”Tidy 说：”我知错了。。。”但 Windbreaker 已经挂掉电话了。Tidy 很苦恼，这么算他真的会崩溃的，聪明的读者，你能写个程序帮他完成这项工作吗？不过如果你的程序效率不够高的话，Tidy 还是会受到 Derek 的责骂的.<br><br><br><br><br><br>Input<br><br>第一行一个整数 T，表示有 T 组数据。<br><br>每组数据第一行一个正整数 N（N&lt;=50000）,表示敌人有 N 个工兵营地，接下来有 N 个正整数,第 i 个正整数 ai 代表第 i 个工兵营地里开始时有 ai 个人（1&lt;=ai&lt;=50）。<br><br>接下来每行有一条命令，命令有 4 种形式：<br><br>(1) Add i j,i 和 j 为正整数,表示第 i 个营地增加 j 个人（j 不超过 30）<br><br>(2)Sub i j ,i 和 j 为正整数,表示第 i 个营地减少 j 个人（j 不超过 30）;<br><br>(3)Query i j ,i 和 j 为正整数,i&lt;=j，表示询问第 i 到第 j 个营地的总人数;<br><br>(4)End 表示结束，这条命令在每组数据最后出现;<br><br>每组数据最多有 40000 条命令<br><br><br><br><br><br>Output<br><br>对第 i 组数据,首先输出“Case i:”和回车,<br><br>对于每个 Query 询问，输出一个整数并回车,表示询问的段中的总人数,这个数保持在 int 以内。<br><br><br><br><br><br>Sample Input<br><br><br><br>1<br><br>10<br><br>1 2 3 4 5 6 7 8 9 10<br><br>Query 1 3<br><br>Add 3 6<br><br>Query 2 7<br><br>Sub 10 2<br><br>Add 6 3<br><br>Query 3 10<br><br>End<br><br><br><br><br><br><br><br>Sample Output<br><br><br><br>Case 1:<br><br>6<br><br>33<br><br>59<br><br><br><br></p>

<h1 id="代码："><a href="#代码：" class="headerlink" title="代码："></a>代码：</h1><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// oj.cpp : 定义控制台应用程序的入口点。</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;numeric&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> tree[<span class="number">900000</span>];</span><br><span class="line"><span class="keyword">int</span> L[<span class="number">50010</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> now, <span class="keyword">int</span> beg, <span class="keyword">int</span> end)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (end - beg &lt;= <span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        tree[now] = L[beg];</span><br><span class="line">        <span class="keyword">return</span> tree[now];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">int</span> mid = beg + (end - beg) / <span class="number">2</span>;</span><br><span class="line">    tree[now] = insert(now * <span class="number">2</span>, beg, mid)</span><br><span class="line">        + insert(now * <span class="number">2</span> + <span class="number">1</span>, mid, end);</span><br><span class="line">    <span class="keyword">return</span> tree[now];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> now, <span class="keyword">int</span> beg, <span class="keyword">int</span> end, <span class="keyword">int</span> i, <span class="keyword">int</span> x)</span></span></span><br><span class="line"><span class="function"></span>&#123;<span class="comment">//i&gt;=0</span></span><br><span class="line">    tree[now] += x;</span><br><span class="line">    <span class="keyword">if</span> (end-beg &lt;= <span class="number">1</span>)</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">int</span> mid = beg + (end - beg) / <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">if</span> (mid &gt; i)</span><br><span class="line">        add(now * <span class="number">2</span>, beg, mid, i, x);</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        add(now * <span class="number">2</span> + <span class="number">1</span>, mid, end, i, x);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sumt</span><span class="params">(<span class="keyword">int</span> now, <span class="keyword">int</span> beg, <span class="keyword">int</span> end, <span class="keyword">int</span> i, <span class="keyword">int</span> j)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (beg &gt;= j || end &lt;= i)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (beg &gt;= i &amp;&amp; end &lt;= j)</span><br><span class="line">        <span class="keyword">return</span> tree[now];</span><br><span class="line">    <span class="keyword">int</span> mid = beg + (end - beg) / <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">return</span> sumt(now * <span class="number">2</span>, beg, mid, i, j)</span><br><span class="line">        + sumt(now * <span class="number">2</span> + <span class="number">1</span>, mid, end, i, j);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    ios::sync_with_stdio(<span class="literal">false</span>);</span><br><span class="line">    <span class="built_in">cin</span>.tie(<span class="literal">NULL</span>);</span><br><span class="line">    <span class="keyword">int</span> N, T;</span><br><span class="line">    <span class="built_in">cin</span> &gt;&gt; T;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; T;++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; <span class="string">"Case "</span> &lt;&lt; i+<span class="number">1</span> &lt;&lt; <span class="string">":\n"</span>;</span><br><span class="line">        <span class="built_in">cin</span> &gt;&gt; N;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; N;++i)</span><br><span class="line">            <span class="built_in">cin</span> &gt;&gt; L[i];</span><br><span class="line">        insert(<span class="number">1</span>, <span class="number">0</span>, N);</span><br><span class="line">        <span class="built_in">string</span> S;</span><br><span class="line">        <span class="keyword">while</span> (<span class="built_in">cin</span> &gt;&gt; S&amp;&amp;S != <span class="string">"End"</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">int</span> i, j;</span><br><span class="line">            <span class="built_in">cin</span> &gt;&gt; i &gt;&gt; j;</span><br><span class="line">            --i;</span><br><span class="line">            <span class="keyword">if</span> (S == <span class="string">"Add"</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                add(<span class="number">1</span>, <span class="number">0</span>, N, i, j);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">if</span> (S == <span class="string">"Sub"</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                add(<span class="number">1</span>, <span class="number">0</span>, N, i, -j);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="built_in">cout</span> &lt;&lt; sumt(<span class="number">1</span>, <span class="number">0</span>, N, i, j) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="解析："><a href="#解析：" class="headerlink" title="解析："></a>解析：</h1><p>刚学了线段树就拿来练手了:trollface:</p>
<p>这里总结一下线段树的模板。</p>
<h2 id="建树"><a href="#建树" class="headerlink" title="建树"></a>建树</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> now, <span class="keyword">int</span> beg, <span class="keyword">int</span> end)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (end - beg &lt;= <span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        tree[now] = L[beg];</span><br><span class="line">        <span class="keyword">return</span> tree[now];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">int</span> mid = beg + (end - beg) / <span class="number">2</span>;</span><br><span class="line">    tree[now] = insert(now * <span class="number">2</span>, beg, mid)</span><br><span class="line">        + insert(now * <span class="number">2</span> + <span class="number">1</span>, mid, end);</span><br><span class="line">    <span class="keyword">return</span> tree[now];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>虽然我在代码里面写的是 insert，但其实是 build 过程。</p>
<p>形参：</p>
<ul>
<li><strong>now:</strong> 树中节点的指针</li>
<li><strong>beg:</strong> 这个节点表示的区间的开头的索引</li>
<li><strong>end:</strong> 这个节点表示的区间的结尾的下一个元素索引</li>
</ul>
<p>注意本代码的所有 end 都是超尾，和 stl 的 end 类似。</p>
<p>全局变量：</p>
<ul>
<li><strong>tree[]:</strong> 二叉树</li>
<li><strong>L[]:</strong> 要求和的区间中各个元素的值</li>
</ul>
<p>树中节点的值表示此区间的和，如果是 RMQ 问题的题的话，值就是区间中的最大值。 这个函数找到 beg 和 end 的中点，将区间分成两部分，然后把这两部分交给左右子 节点递归解决。如果当前区间只剩下了一个值，就结束递归，节点的值就是区间中这个 元素的值。</p>
<h2 id="更新"><a href="#更新" class="headerlink" title="更新"></a>更新</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> now, <span class="keyword">int</span> beg, <span class="keyword">int</span> end, <span class="keyword">int</span> i, <span class="keyword">int</span> x)</span></span></span><br><span class="line"><span class="function"></span>&#123;<span class="comment">//i&gt;=0</span></span><br><span class="line">    tree[now] += x;</span><br><span class="line">    <span class="keyword">if</span> (end-beg &lt;= <span class="number">1</span>)</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">int</span> mid = beg + (end - beg) / <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">if</span> (mid &gt; i)</span><br><span class="line">        add(now * <span class="number">2</span>, beg, mid, i, x);</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        add(now * <span class="number">2</span> + <span class="number">1</span>, mid, end, i, x);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>形参：</p>
<ul>
<li><strong>now beg end:</strong> 与上文一样</li>
<li><strong>i:</strong> 区间中要更新的值的索引</li>
<li><strong>x:</strong> 要增加（或者减少）的值</li>
</ul>
<p>因为在线段树中上一个节点存储了两个子节点的和，而要更新的那个元素必然属于两个 子节点之一。因此我们在更新了当前节点之后需要判断要更改的元素属于哪个子节点， 并且递归处理。如果是 RMQ 问题的题的话，我们需要在当前区间只有一个值时更新 区间，然后在子节点的递归结束后再根据两个子节点的值来判断当前的节点的值。</p>
<h2 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sumt</span><span class="params">(<span class="keyword">int</span> now, <span class="keyword">int</span> beg, <span class="keyword">int</span> end, <span class="keyword">int</span> i, <span class="keyword">int</span> j)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (beg &gt;= j || end &lt;= i)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (beg &gt;= i &amp;&amp; end &lt;= j)</span><br><span class="line">        <span class="keyword">return</span> tree[now];</span><br><span class="line">    <span class="keyword">int</span> mid = beg + (end - beg) / <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">return</span> sumt(now * <span class="number">2</span>, beg, mid, i, j)</span><br><span class="line">        + sumt(now * <span class="number">2</span> + <span class="number">1</span>, mid, end, i, j);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>形参：</p>
<ul>
<li><strong>now beg end:</strong> 同上</li>
<li><strong>i j:</strong> 要查询的区间的开头和结尾。</li>
</ul>
<p>找到树中恰好可以拼出 <code>[i,j)</code> 区间的区间。需要在左右子树中分别寻找可以拼出指定的 区间的部分。由于节点可以表示的最小的区间长度为 1，所以在 <code>log n</code> 步之后一定可以 正好拼出这个区间来。函数返回的是（当前节点表示的）beg 到 end 区间内可以匹配 i 到 j 区间的所有值的和。至于 RMQ 问题，只要把 return 语句中的 <code>+</code> 改成 max 函数就可以了。</p>

      </section>
      <div class="license">
        <a href="http://creativecommons.org/licenses/by-sa/3.0/" rel="license" target="_blank">
          <img src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" style="border-width:0"
               alt="Creative Commons License" class="center">
        </a>
      </div>
      <p>
        本作品采用
        <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank">
          署名-相同方式共享 4.0 国际
        </a>
        进行许可。欢迎转载、使用、重新发布，但务必保留文章署名
        “不科学的科学君” (Liu233w) 与博客链接：
        <a href="https://liu233w.github.io">https://liu233w.github.io</a>
        ，基于本文修改后的作品务必以相同的许可发布。如有任何疑问，请
        <a href="mailto:wwwlsmcom@outlook.com" target="_blank">与我联系</a>
        。
      </p>
    </article>
    
    <!-- disqus 评论框 start -->
    <div class="comment">
      <div id="disqus_thread" class="disqus-thread">
        <i>加载评论框需要翻墙</i>
      </div>
    </div>
    <!-- disqus 评论框 end -->
    
    
    <!-- livere 评论框 start -->
    <div class="comment">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNTE5NS8xMTczMA=="></div>
    </div>
    <!-- livere 评论框 end -->
    
  </div>
  <aside>
    
    <div class="toc-container">
      <h1>目录</h1>
      <div class="content">
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#题目："><span class="toc-number">1.</span> <span class="toc-text">题目：</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#代码："><span class="toc-number">2.</span> <span class="toc-text">代码：</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#解析："><span class="toc-number">3.</span> <span class="toc-text">解析：</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#建树"><span class="toc-number">3.1.</span> <span class="toc-text">建树</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#更新"><span class="toc-number">3.2.</span> <span class="toc-text">更新</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#查询"><span class="toc-number">3.3.</span> <span class="toc-text">查询</span></a></li></ol></li></ol>
      </div>
    </div>
    
  </aside>
</main>

<!-- disqus 公共JS代码 -->
<script type="text/javascript">
  /* * * CONFIGURATION VARIABLES * * */
  var disqus_shortname = "liu233w";
  var disqus_identifier = "https://liu233w.github.io/2016/04/14/hdu-1166.org/";
  var disqus_url = "https://liu233w.github.io/2016/04/14/hdu-1166.org/";

  isAgent(getDisqus)

  // determine user agent in China
  function isAgent(cb) {
    var url = '//graph.facebook.com/feed?callback=h';
    var xhr = new XMLHttpRequest();
    var called = false;
    xhr.open('GET', url);
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 && xhr.status === 200) {
        called = true;
        cb(true);
      }
    };
    xhr.send();
    // timeout 1s, this facebook API is very fast.
    setTimeout(function () {
      if (!called) {
        xhr.abort();
        cb(false)
      }
    }, 1000);
  }

  function getDisqus(isAgent) {
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq)
  }
</script>
<!-- disqus 公共JS代码 end -->


<script type="text/javascript">
  (function (d, s) {
    var j, e = d.getElementsByTagName(s)[0];

    if (typeof LivereTower === 'function') {
      return;
    }

    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;

    e.parentNode.insertBefore(j, e);
  })(document, 'script');
</script>


  <footer>
  <div class="copyright">
    <div>
      &copy; 2019 | Powered by <a href="https://hexo.io" target="_blank">Hexo</a>&nbsp
    </div>
    <div>
      Theme by <a href="https://github.com/lewis-geek/hexo-theme-Aath" target="_blank">Aath</a>
    </div>
  </div>
</footer>


<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="/lib/in-view.min.js"></script>
<script src="/lib/lodash.min.js"></script>
<script>
  var isDown = true
  var oldY = 0
  inView.offset(50)

  document.body.addEventListener('touchstart', function(){});
  
  window.addEventListener('scroll', _.throttle(e => {
    var currentY = window.scrollY
    if((oldY - currentY) < 0) {
      isDown = true
    } else {
      isDown = false
    }
    oldY = currentY
  }, 250))

  $("article img").each(function() {
      var strA = "<a data-fancybox='gallery' href='" + this.src + "'></a>";
      $(this).wrapAll(strA);
  });

  $('.toc-link').each(function() {
      var href = $(this).attr("href");
      
      inView(href).on('exit', () => {
        if (isDown) {
          handleActive(href)
        }
      })

      inView(href).on('enter', () => {
        if (!isDown) {
          handleActive(href)
        }
      })

      this.onclick = function(e) {
        var pos = $(href).offset().top - 10;
        $("html,body").animate({scrollTop: pos}, 300);
        setTimeout(() => {
          handleActive(href)
        }, 350)
        return false
      }
  })

  function handleActive(href) {
    document.querySelectorAll('.toc-link').forEach(elm => {
      elm.classList.remove('active')
    })
    document.querySelector(".toc [href='"+ href +"']").classList.add('active')
  }
</script>
<script src="/lib/jquery.fancybox.min.js"></script>

<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

</body>
</html>
